Imports System
Imports System.Web
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports DotNetNuke
Imports Bring2mind.DNN.Modules.UserFiles.Business

Namespace Bring2mind.DNN.Modules.UserFiles.Controls

 Public Class UserFilesList
  Inherits ModuleBase

#Region " Web Form Designer Generated Code "

  'This call is required by the Web Form Designer.
  <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()

  End Sub
  Protected WithEvents dgFiles As System.Web.UI.WebControls.DataGrid
  Protected WithEvents cmdCancel As System.Web.UI.WebControls.LinkButton

  'NOTE: The following placeholder declaration is required by the Web Form Designer.
  'Do not delete or move it.
  Private designerPlaceholderDeclaration As System.Object

  Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
   'CODEGEN: This method call is required by the Web Form Designer
   'Do not modify it using the code editor.
   InitializeComponent()
  End Sub

#End Region

  Public OurDirectory As String
  Private OurUploadDirectory As String

  Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

   OurDirectory = "http://" & PortalAlias.HTTPAlias & Me.PortalSettings.HomeDirectory & "/UserFiles/"
   OurUploadDirectory = Me.UserFilesSettings.UploadDirectory

   If Not Me.IsPostBack Then
    DotNetNuke.Services.Localization.Localization.LocalizeDataGrid(dgFiles, Me.LocalResourceFile)
    dgFiles.PageSize = Me.UserFilesSettings.GridSize
    BindGrid()
   End If
  End Sub

  Private Sub BindGrid()
   Dim dv As DataView = New DataView(DotNetNuke.Common.Globals.ConvertDataReaderToDataTable(Bring2mind.DNN.Modules.UserFiles.Data.DataProvider.Instance().GetAllPortalFiles(PortalId)))
   dv.Sort = "FullName, FileName"
   dgFiles.DataSource = dv
   If dv.Count > 0 Then
    If dgFiles.CurrentPageIndex > (dv.Count - 1) / Me.UserFilesSettings.GridSize Then
     dgFiles.CurrentPageIndex = Int((dv.Count - 1) / Me.UserFilesSettings.GridSize)
    End If
    dgFiles.Visible = True
   Else
    dgFiles.Visible = False
   End If
   dgFiles.DataBind()
  End Sub

  Private Sub dgFiles_PageIndexChanged(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridPageChangedEventArgs) Handles dgFiles.PageIndexChanged
   dgFiles.CurrentPageIndex = e.NewPageIndex
   BindGrid()
  End Sub

  Public Function FormatSize(ByVal Bts As Integer) As String
   If Bts > 1024 ^ 3 Then
    Return (Bts / 1024 ^ 3).ToString("0.0") & " GB"
   ElseIf Bts > 1024 ^ 2 Then
    Return (Bts / 1024 ^ 2).ToString("0.0") & " MB"
   ElseIf Bts > 1024 Then
    Return (Bts / 1024).ToString("0.0") & " KB"
   Else
    Return Bts.ToString("0,000") & " B"
   End If
  End Function

  Private Sub dgFiles_DeleteCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) Handles dgFiles.DeleteCommand
   Try
    Dim objFiles As New FilesController
    Dim objFile As FilesInfo = objFiles.GetFiles(dgFiles.DataKeys(e.Item.ItemIndex), PortalId)
    If Not objFile Is Nothing Then
     IO.File.Delete(OurUploadDirectory & "User" & objFile.UserId & "/" & objFile.FileName)
     objFiles.DeleteFiles(objFile.id)
    End If

    BindGrid()

   Catch exc As Exception 'Module failed to load
    DotNetNuke.Services.Exceptions.ProcessModuleLoadException(Me, exc)
   End Try

  End Sub

  Private Sub dgFiles_ItemCreated(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) Handles dgFiles.ItemCreated
   Try
    Dim cmdDelete As Control = e.Item.FindControl("cmdDelete")

    If Not cmdDelete Is Nothing Then
     CType(cmdDelete, ImageButton).Attributes.Add("onClick", "javascript: return confirm('Are You Sure You Wish To Delete This Item ?')")
    End If

   Catch exc As Exception 'Module failed to load
    DotNetNuke.Services.Exceptions.ProcessModuleLoadException(Me, exc)
   End Try
  End Sub

  Private Sub cmdCancel_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles cmdCancel.Click
   Try
    Response.Redirect(DotNetNuke.Common.NavigateURL(), True)
   Catch exc As Exception
    DotNetNuke.Services.Exceptions.ProcessModuleLoadException(Me, exc)
   End Try
  End Sub
 End Class

End Namespace
